package com.vf.admin.miaodong.controller;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.param.UpdateDongTaiWebParam;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.VipOrderService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.entity.VipOrder;
import com.vf.admin.miaodong.service.AfterUserService;
import com.vf.admin.miaodong.service.AfterVipOrderService;
import com.vf.admin.platform.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/8/9 20:36
 */
@Controller
@RequestMapping("/after/viporder")
public class AfterVipOrderController extends BaseController {
    @Autowired
    private AfterVipOrderService afterVipOrderService;
    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private AfterUserService appUserService;
    @Autowired
    RedisUtil redisUtil;


    /**
     * 手动修改vip
     */
    @PostMapping(value = "/updateVipWeb")
    @ResponseBody
    public Result updateVipWeb(@RequestBody UpdateDongTaiWebParam param) {
        String[] split = param.getUserIds().split(",");
        List<String> userIdList = Arrays.asList(split);
        userIdList.forEach(e -> {

            vipOrderService.updateVipWeb(Integer.valueOf(e),param.getMonthNum());
        });
        return this.sucessJSON("成功");
    }



    /**
     * 获取所有订单
     */
    @RequestMapping("/list")
    public String getAllUser(SplitPageBean splitPage,
                             String nick, Integer type,
                             Integer status, Integer refundStatus, String phone,
                             String orderNo, String startdate,
                             String enddate, String price) {
        Map<String, Object> map = new HashMap<>();
        map.put("nick", nick);//用户名称
        map.put("phone", phone);//用户电话
        map.put("type", type);//订单类型  1支付宝  2微信
        map.put("status", status);//状态  1待付款  2已付款
        map.put("refundStatus", refundStatus);//状态  1待付款  2已付款
        map.put("orderNo", orderNo);//订单号
        map.put("startdate", startdate);
        map.put("enddate", enddate);
        map.put("orderType", "2");
        map.put("price", price);
        Map<Integer,String> statesMap = Constants.getOrderTypeMap();
        Map<Integer, String> userStatusMap = Constants.getVipOrderStatusMap();
        Map<Integer, String> refundStatusMap = Constants.getVipOrderRefundStatusMap();
        List<VipOrder> maps = afterVipOrderService.findList(map, splitPage);
        request.setAttribute("nick",nick);
        request.setAttribute("phone",phone);
        request.setAttribute("type",type);
        request.setAttribute("status",status);
        request.setAttribute("refundStatus",refundStatus);
        request.setAttribute("orderNo",orderNo);
        request.setAttribute("statesMap",statesMap);
        request.setAttribute("userStatusMap",userStatusMap);
        request.setAttribute("refundStatusMap",refundStatusMap);
        request.setAttribute("startdate",startdate);
        request.setAttribute("enddate",enddate);
        request.setAttribute("price", price);
        request.setAttribute("list",maps);
        request.setAttribute("splitPage",splitPage);
        User user = (User) request.getSession(false).getAttribute(Constants.access_token);
        request.setAttribute("canRefund",user.getId() ==101?true:false);
        return this.sucess("/web/after/viporder/list");
    }

    /**
     * 获取所有订单
     */
    @RequestMapping("/ticketlist")
    public String getTicketList(SplitPageBean splitPage,
                             String nick, Integer type,
                             Integer status, Integer refundStatus, String phone,
                             String orderNo, String startdate,
                             String enddate, String price) {
        Map<String, Object> map = new HashMap<>();
        map.put("nick", nick);//用户名称
        map.put("phone", phone);//用户电话
        map.put("type", type);//订单类型  1支付宝  2微信
        map.put("status", status);//状态  1待付款  2已付款
        map.put("refundStatus", refundStatus);//状态  1待付款  2已付款
        map.put("orderNo", orderNo);//订单号
        map.put("startdate", startdate);
        map.put("enddate", enddate);
        map.put("orderType", "3");
        map.put("price", price);
        Map<Integer,String> statesMap = Constants.getOrderTypeMap();
        Map<Integer, String> userStatusMap = Constants.getVipOrderStatusMap();
        Map<Integer, String> refundStatusMap = Constants.getVipOrderRefundStatusMap();
        List<VipOrder> maps = afterVipOrderService.findList(map, splitPage);
        request.setAttribute("nick",nick);
        request.setAttribute("phone",phone);
        request.setAttribute("type",type);
        request.setAttribute("status",status);
        request.setAttribute("refundStatus",refundStatus);
        request.setAttribute("orderNo",orderNo);
        request.setAttribute("statesMap",statesMap);
        request.setAttribute("userStatusMap",userStatusMap);
        request.setAttribute("refundStatusMap",refundStatusMap);
        request.setAttribute("startdate",startdate);
        request.setAttribute("enddate",enddate);
        request.setAttribute("price", price);
        request.setAttribute("list",maps);
        request.setAttribute("splitPage",splitPage);

        User user = (User) request.getSession(false).getAttribute(Constants.access_token);
        request.setAttribute("canRefund",user.getId() ==101?true:false);
        return this.sucess("/web/after/viporder/ticketlist");
    }

    /**
     * 获取所有订单
     */
    @RequestMapping("/refund")
    @ResponseBody
    public Result refund(String orderNo) {
        VipOrder vipOrder = vipOrderService.infoByOrderNo(orderNo);
        if(ObjectUtil.isEmpty(vipOrder)){
            return this.errorJSON("订单不存在");
        }
        if(1 != vipOrder.getStatus()){
            return this.errorJSON("订单状态错误");
        }
        AppUser appUser = appUserService.get(vipOrder.getUserId());
        if(ObjectUtil.isEmpty(appUser)){
            return this.errorJSON("用户不存在");
        }
        //1.修改订单状态，退款时间
        vipOrderService.refund(vipOrder);
        //2.禁用用户
        appUserService.upstate(appUser.getId(), 2, 0);
        String key = String.format(RedisKeys.USER, appUser.getId());
        redisUtil.del(key);
        String keys = String.format(RedisKeys.userIds, appUser.getId());
        redisUtil.del(keys);
        return this.sucessJSON("保存成功");
    }
}
